<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>学生信息管理</title>
  <link rel="stylesheet" href="css/index.css" />
</head>

<body>
  <h1>新增学员</h1>
  <form class="info" autocomplete="off">
    姓名：<input type="text" class="uname" name="uname" />
    年龄：<input type="text" class="age" name="age" />

    性别：<select name="gender" class="gender">
      <option value="男">男</option>
      <option value="女">女</option>
    </select>
    薪资：<input type="text" class="salary" name="salary" />
    就业城市：<select name="city" class="city">
      <option value="北京">北京</option>
      <option value="上海">上海</option>
      <option value="广州">广州</option>
      <option value="深圳">深圳</option>
      <option value="曹县">曹县</option>
    </select>
    <button class="add">录入</button>
  </form>
  <h1>就业版</h1>
  <table>
    <thead>
      <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>薪资</th>
        <th>就业城市</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <!-- <tr>
        <td>1001</td>
        <td>欧阳霸天</td>
        <td>19</td>
        <td>男</td>
        <td>15000</td>
        <td>上海</td>
        <td><a href="javascript:">删除</a></td>
      </tr> -->
    </tbody>
  </table>
  <script>
    const data = localStorage.getItem('student-data')

    const arr = data ? JSON.parse(data) : []

    console.log(arr)

    const tbody = document.querySelector('tbody')

    function render() {

      const trArr = arr.map(function (item, index) {
        return `
        <tr>
          <td>${item.stuId}</td>
          <td>${item.uname}</td>
          <td>${item.age}</td>
          <td>${item.gender}</td>
          <td>${item.salary}</td>
          <td>${item.city}</td>
          <td>
            <a href="javascript:" data-id=${index}>删除</a>
          </td>
        </tr>
        `
      })

      tbody.innerHTML = trArr.join('')
    }

    render()
    // 4.录入模块
    // debugger
    const info = document.querySelector('.info')
    //获取表单form 里面带有 name属性的元素
    const items = info.querySelectorAll('[name]')
    info.addEventListener('submit', function (e) {
      // debugger
      //阻止提交
      e.preventDefault()
      console.log('表单提交')
      //声明空的对象
      const obj = {}
      obj.stuId = arr.length ? arr[arr.length - 1].stuId + 1 : 1
      //非空判断
      for (let i = 0; i < items.length; i++) {
        const item = items[i];
        if (items[i].value === '') {
          return alert('输入内容不能为空')
        }
        obj[item.name] = item.value
      }
      arr.push(obj)

      localStorage.setItem('student-data', JSON.stringify(arr))

      //渲染页面
      render()
      //重置表单
      this.reset()

    })

    tbody.addEventListener('click', function (e) {
      if (e.target.tagName === 'A') {
        arr.splice(e.target.dataset.id, 1)
        localStorage.setItem('student-data', JSON.stringify(arr))
        render()
      }
    })
  </script>

</body>

</html>